#include <iostream>
#include <vector>
using namespace std;

int m,n,a[1005],flag;
vector<int> ans;

void dfs(int u){
    if(u*2>=1&&u*2<=n){
        if(flag==1&&a[u*2]>a[u])    flag = -1;
        if(flag==0&&a[u*2]<a[u])    flag = -1;
        dfs(u*2);
    }
    if(u*2+1>=1&&u*2+1<=n){
        if(flag==1&&a[u*2+1]>a[u])    flag = -1;
        if(flag==0&&a[u*2+1]<a[u])    flag = -1;
        dfs(u*2+1);
    }
    ans.push_back(a[u]);
}

signed main()
{
    cin>>m>>n;
    for(int i=0;i<m;i++){
        for(int j=1;j<=n;j++)    cin>>a[j];
        if(a[1]>a[2])    flag = 1;
        else    flag = 0;
        ans.clear();
        dfs(1);
        if(flag==1)    cout<<"Max Heap\n";
        else if(flag==0)    cout<<"Min Heap\n";
        else    cout<<"Not Heap\n";
        cout<<ans[0];
        for(int j=1;j<ans.size();j++)    cout<<" "<<ans[j];
        cout<<"\n";
    }
    return 0;
}
